package org.liaohailong.library.widget.ptr;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.liaohailong.library.R;

/**
 * Author: liaohailong
 * Date: 2019/5/9
 * Time: 20:46
 * Description: 下拉刷新的头部View
 **/
public class PullToRefreshHeaderView extends FrameLayout implements IPullToRefreshHeader {

    private TextView arrowView;
    private TextView infoView;
    private TextView progressView;

    private int viewHeight = 0;
    private boolean pullDown = true;
    private ObjectAnimator arrowAnim;
    private ObjectAnimator refreshingAnim;

    private boolean isRefreshing = false;

    public PullToRefreshHeaderView(@NonNull Context context) {
        this(context, null);
    }

    public PullToRefreshHeaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullToRefreshHeaderView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.layout_default_ptr_header_view, this, true);

        arrowView = findViewById(R.id.arrow_img);
        infoView = findViewById(R.id.info_txt);
        progressView = findViewById(R.id.progress_txt);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewHeight = h;
    }

    @Override
    public void onPulling(PullToRefreshHandler handler) {
        int scrollDistance = handler.getScrollDistance();
        if (viewHeight > 0) {
            int percent = (int) (100 * (scrollDistance * 1f / viewHeight * 1f));
            percent = Math.min(100, percent);
            String progressText = "下拉进度 = " + percent + "%";
            progressView.setText(progressText);
            if (isRefreshing) {
                if (refreshingAnim == null) {
                    refreshingAnim = ObjectAnimator.ofFloat(arrowView, "rotation", 0f, 360f);
                    refreshingAnim.setDuration(300);
                    refreshingAnim.setRepeatCount(ValueAnimator.INFINITE);
                    refreshingAnim.start();
                }
                infoView.setText("正在刷新...");
                return;
            }
            if (scrollDistance < viewHeight) {
                infoView.setText("下拉刷新");
                if (pullDown) {
                    stopAnim();
                    arrowAnim = ObjectAnimator.ofFloat(arrowView, "rotation", 0f);
                    arrowAnim.setDuration(200);
                    arrowAnim.start();
                    pullDown = false;
                }
            } else {
                infoView.setText("松手刷新");
                if (!pullDown) {
                    stopAnim();
                    arrowAnim = ObjectAnimator.ofFloat(arrowView, "rotation", 180f);
                    arrowAnim.setDuration(200);
                    arrowAnim.start();
                    pullDown = true;
                }
            }
        }
    }

    private void stopAnim() {
        if (arrowAnim != null && arrowAnim.isRunning()) {
            arrowAnim.end();
            arrowAnim.cancel();
            arrowAnim = null;
        }
        if (refreshingAnim != null && refreshingAnim.isRunning()) {
            refreshingAnim.end();
            refreshingAnim.cancel();
            refreshingAnim = null;
        }
    }

    @Override
    public void onBeforeRefresh() {
        isRefreshing = true;
    }

    @Override
    public void onRefreshing() {
        isRefreshing = true;
    }

    @Override
    public void onRefreshCancelled() {
        isRefreshing = false;
        stopAnim();
    }

    @Override
    public void onAfterRefresh() {
        isRefreshing = false;
        stopAnim();
    }

    @Override
    public void onReset() {
        pullDown = true;
        stopAnim();
    }
}
